﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using AD.Model;
using System.Data.SqlClient;

namespace AD.DAL
{
    public class SystemSettingDAL : DALBase
    {
        public static SystemSettingMod QueryFirst()
        {
            try
            {
                DataTable dt = QueryFilterToTable("");
                if (dt == null || dt.Rows.Count <= 0) return null;
                foreach (DataRow row in dt.Rows)
                {
                    return SetModel(row);
                }
            }
            catch
            {
            }
            return null;
        }

        public static IList<SystemSettingMod> QueryFilterToList(string filter)
        {
            try
            {
                DataTable dt = QueryFilterToTable(filter);
                if (dt == null || dt.Rows.Count <= 0) return null;
                IList<SystemSettingMod> settings = new List<SystemSettingMod>();
                foreach (DataRow row in dt.Rows)
                {
                    SystemSettingMod setting = SetModel(row);
                    settings.Add(setting);
                }
                return settings;
            }
            catch
            {
            }
            return null;
        }

        public static DataTable QueryFilterToTable(string filter)
        {
            try
            {
                string sql = "Select * from tab_system_setting_info ";
                if (!string.IsNullOrEmpty(filter))
                    sql += " where " + filter;
                return ExecuteDataTable(sql);
            }
            catch
            {
            }
            return null;
        }


        public static int Update(SystemSettingMod systemSetting)
        {
            try
            {
                string sql = "update tab_system_setting_info set EverydayShopBalancePoint=@EverydayShopBalancePoint,MoneyScale=@MoneyScale,LowPoint=@LowPoint,IsInMoneyAudit=@IsInMoneyAudit,RewardBalancePoint=@RewardBalancePoint,IsValidReward=@IsValidReward,IsChangeAudit=@IsChangeAudit,IsEverydayChargeback=@IsEverydayChargeback,EverydayKeyWordBalancePoint=@EverydayKeyWordBalancePoint where SettingId=@SettingId";
                SqlParameter[] sqlParams = GetSqlParameter(systemSetting);
                return ExecuteNonQuery(sql, sqlParams);
            }
            catch
            {
                return 0;
            }
        }

        #region 模型

        static SystemSettingMod SetModel(DataRow row)
        {
            SystemSettingMod setting = new SystemSettingMod();
            setting.SettingId = int.Parse(row["SettingId"].ToString());

            if (row["IsEverydayChargeback"] != DBNull.Value)
                setting.IsEverydayChargeback = bool.Parse(row["IsEverydayChargeback"].ToString());

            if (row["EverydayKeyWordBalancePoint"] != DBNull.Value)
                setting.EverydayKeyWordBalancePoint = int.Parse(row["EverydayKeyWordBalancePoint"].ToString());

            if (row["IsChangeAudit"] != DBNull.Value)
                setting.IsChangeAudit = bool.Parse(row["IsChangeAudit"].ToString());

            if (row["IsChangeAudit"] != DBNull.Value)
                setting.IsChangeAudit = bool.Parse(row["IsChangeAudit"].ToString());

            if (row["IsAddAudit"] != DBNull.Value)
                setting.IsAddAudit = bool.Parse(row["IsAddAudit"].ToString());

            if (row["IsValidReward"] != DBNull.Value)
                setting.IsValidReward = bool.Parse(row["IsValidReward"].ToString());

            if (row["IsInMoneyAudit"] != DBNull.Value)
                setting.IsInMoneyAudit = bool.Parse(row["IsInMoneyAudit"].ToString());

            if (row["RewardBalancePoint"] != DBNull.Value)
                setting.RewardBalancePoint = int.Parse(row["RewardBalancePoint"].ToString());
            
            if (row["SettingId"] != DBNull.Value)
                setting.SettingId = int.Parse(row["SettingId"].ToString());

            if (row["EverydayShopBalancePoint"] != DBNull.Value)
                setting.EverydayShopBalancePoint = int.Parse(row["EverydayShopBalancePoint"].ToString());

            if (row["MoneyScale"] != DBNull.Value)
                setting.MoneyScale = double.Parse(row["MoneyScale"].ToString());

            if (row["LowPoint"] != DBNull.Value)
                setting.LowPoint = int.Parse(row["LowPoint"].ToString());
            
            return setting;
        }

        static SqlParameter[] GetSqlParameter(SystemSettingMod systemSetting)
        {
            List<SqlParameter> param = new List<SqlParameter>();
            SqlParameter SettingId = new SqlParameter("@SettingId", SqlDbType.Int, 4, "SettingId");
            SettingId.Value = systemSetting.SettingId;
            param.Add(SettingId);

            SqlParameter LowPoint = new SqlParameter("@LowPoint", SqlDbType.Int, 4, "LowPoint");
            LowPoint.Value = systemSetting.LowPoint;
            param.Add(LowPoint);

            SqlParameter MoneyScale = new SqlParameter("@MoneyScale", SqlDbType.Float, 8, " MoneyScale");
            MoneyScale.Value = systemSetting.MoneyScale;
            param.Add(MoneyScale);

            SqlParameter EverydayKeyWordBalancePoint = new SqlParameter("@EverydayKeyWordBalancePoint", SqlDbType.Int, 4, "EverydayKeyWordBalancePoint");
            EverydayKeyWordBalancePoint.Value = systemSetting.EverydayKeyWordBalancePoint;
            param.Add(EverydayKeyWordBalancePoint);

            SqlParameter EverydayShopBalancePoint = new SqlParameter("@EverydayShopBalancePoint", SqlDbType.Int, 4, "EverydayShopBalancePoint");
            EverydayShopBalancePoint.Value = systemSetting.EverydayShopBalancePoint;
            param.Add(EverydayShopBalancePoint);

            SqlParameter IsChangeAudit = new SqlParameter("@IsChangeAudit", SqlDbType.Bit, 1, "IsChangeAudit");
            IsChangeAudit.Value = systemSetting.IsChangeAudit;
            param.Add(IsChangeAudit);

            SqlParameter IsEverydayChargeback = new SqlParameter("@IsEverydayChargeback", SqlDbType.Bit, 1, "IsEverydayChargeback");
            IsEverydayChargeback.Value = systemSetting.IsEverydayChargeback;
            param.Add(IsEverydayChargeback);

            SqlParameter IsValidReward = new SqlParameter("@IsValidReward", SqlDbType.Bit, 1, "IsValidReward");
            IsValidReward.Value = systemSetting.IsValidReward;
            param.Add(IsValidReward);

            SqlParameter IsInMoneyAudit = new SqlParameter("@IsInMoneyAudit", SqlDbType.Bit, 1, "IsInMoneyAudit");
            IsInMoneyAudit.Value = systemSetting.IsInMoneyAudit;
            param.Add(IsInMoneyAudit);

            SqlParameter RewardBalancePoint = new SqlParameter("@RewardBalancePoint", SqlDbType.Int, 4, "RewardBalancePoint");
            RewardBalancePoint.Value = systemSetting.RewardBalancePoint;
            param.Add(RewardBalancePoint);

            SqlParameter IsAddAudit = new SqlParameter("@IsAddAudit", SqlDbType.Bit, 1, "IsAddAudit");
            IsAddAudit.Value = systemSetting.IsAddAudit;
            param.Add(IsAddAudit);

            //SqlParameter EverydayShopBalancePoint = new SqlParameter("@EverydayShopBalancePoint", SqlDbType.Int, 4, "EverydayShopBalancePoint");
            //EverydayShopBalancePoint.Value = systemSetting.EverydayShopBalancePoint;
            //param.Add(EverydayShopBalancePoint);
            return param.ToArray();
        }

        #endregion

    }
}